/**
 * Created with JetBrains PhpStorm.
 * User: Artem
 * Date: 11.03.13
 * Time: 16:45
 * To change this template use File | Settings | File Templates.
 */

Ext.define("Pro.controller.Main", {
    extend: "Ext.app.Controller",

    requires: [
        "Ext.ux.grid.FiltersFeature"
    ],

    views: [
        "EmployeesPanel",
        "ConsolePanel",
        "MainPanel",
        "TreePanel"
    ],
    stores: [
        "Tree",
        "Employees",
        "Languages",
        "Themes"
    ],

    refs: [
        {
            ref: "emplPanel",
            selector: "#emplPanel"
        },
        {
            ref: "updOrRemFormPanel",
            selector: "updateorremoveemployee #updateOrRemoveFormPanel"
        }
    ],

    init: function(){
        this.control({
            "mainpanel #newEmployee": {
                click: this.createAddEmployeeWindow
            },
            "treepanel": {
                itemclick: this.showEmployees
            },
            "employeespanel": {
                itemdblclick: this.onEmployeesPanelDblClick
            },
            "mainpanel #info": {
                click: this.onInfoButtonClick
            },
            "mainpanel #createNewTeamMenuIntem": {
                click: this.onCreateNewTeamMenuIntemClick
            },
            "mainpanel #languagesBox": {
                change: this.onLanguagesBoxChanged
            },
            "mainpanel #themesBox": {
                change: this.onThemesBoxChanged
            }
        });
    },

    createAddEmployeeWindow: function(){
        Ext.create("Pro.view.AddNewEmployee");
    },

    showEmployees: function(tree, record, item, index, e, options){
        var emplPanel = this.getEmplPanel();
        emplPanel.getStore().getProxy().url = "globallogic?text=" + record.data.text + "&leaf=" + record.data.leaf;
        emplPanel.getStore().load();
        emplPanel.setTitle(record.data.text);

        if (record.data.leaf){
            emplPanel.columns[2].show();
            emplPanel.columns[3].show();
            emplPanel.columns[4].show();
        } else {
            emplPanel.columns[2].hide();
            emplPanel.columns[3].hide();
            emplPanel.columns[4].hide();
        }

    },

    onEmployeesPanelDblClick: function(obj, record, item, index, e, eOpts){
        var emplPanel = this.getEmplPanel();
        var tempUrl = emplPanel.getStore().getProxy().url;
        emplPanel.getStore().getProxy().url = "globallogic?text=" + record.data.name + "&leaf=" + record.data.leaf;
        emplPanel.getStore().load();

        if (record.data.age != 0){
            emplPanel.getStore().getProxy().url = tempUrl;
            emplPanel.getStore().load();

            Ext.create("Pro.view.UpdateOrRemoveEmployee", record);
            var updOrRemFormPanel = this.getUpdOrRemFormPanel();
            updOrRemFormPanel.getForm().setValues({
                "id": record.data.id,
                "team": record.data.team,
                "name": record.data.name,
                "age": record.data.age,
                "phone": record.data.phone,
                "married": record.data.married
            });

        } else {
            emplPanel.setTitle(record.data.name);
            if (record.data.leaf){
                emplPanel.columns[2].show();
                emplPanel.columns[3].show();
                emplPanel.columns[4].show();
            } else {
                emplPanel.columns[2].hide();
                emplPanel.columns[3].hide();
                emplPanel.columns[4].hide();
            }
        }

    },

    onInfoButtonClick: function(){
        Ext.MessageBox.alert("Info", "Product: Account Manager v 1.0<br>Server: NodeJS Express<br>DataBase: MySQL5");
    },

    onCreateNewTeamMenuIntemClick: function(){
        Ext.create("Pro.view.CreateNewTeam");
    },

    onLanguagesBoxChanged: function(obj, newValue, oldValue, eOpts){

        Ext.util.Cookies.set("language", newValue);
        location.href = "";
    },

    onThemesBoxChanged: function(obj, newValue, oldValue, eOpts){
        Ext.util.Cookies.set("theme", newValue);
        location.href = "";
    }
});